﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using umbraco.interfaces;
using umbraco.NodeFactory;
using umbraco.cms.businesslogic.web;
using umbraco.cms.businesslogic.property;

namespace idseefeld.com.images
{
    public static class UmbracoProperty
    {
        public static string GetStringProperty(object node, string propName)
        {
            return GetStringProperty(node, propName, false);
        }
        public static string GetStringProperty(object node, string propName, bool recursive)
        {
            string result = "";
            if (node == null)
                return result;

            object parent = null;
            Type nodeType = node.GetType();
            if (nodeType.Equals(typeof(Node)))
            {
                IProperty p = ((Node)node).GetProperty(propName);
                if (recursive && (p == null || String.IsNullOrEmpty(p.Value.ToString())))
                    parent = ((Node)node).Parent;
                result = p != null ? p.Value.ToString() : "";
            }
            else if (nodeType.Equals(typeof(Document)))
            {
                umbraco.cms.businesslogic.property.Property p = ((Document)node).getProperty(propName);
                if (recursive && (p == null || String.IsNullOrEmpty(p.Value.ToString())))
                    parent = ((Document)node).ParentId > -1 ? ((Document)node).Parent : null;
                result = p != null ? p.Value.ToString() : "";
            }
            if (parent == null)
                return result;
            else
                return GetStringProperty(parent, propName, recursive);
        }
        public static int GetIntProperty(object node, string propName)
        {
            return GetIntProperty(node, propName, false);
        }
        public static int GetIntProperty(object node, string propName, bool recursive)
        {
            int result = 0;
            if (node == null)
                return result;

            object parent = null;
            Type nodeType = node.GetType();
            if (nodeType.Equals(typeof(Node)))
            {
                IProperty p = ((Node)node).GetProperty(propName);
                if (recursive && (p == null || String.IsNullOrEmpty(p.Value.ToString())))
                    parent = ((Node)node).Parent;
                if (p != null)
                    int.TryParse(p.Value.ToString(), out result);
            }
            else if (nodeType.Equals(typeof(Document)))
            {
                umbraco.cms.businesslogic.property.Property p = ((Document)node).getProperty(propName);
                if (recursive && (p == null || String.IsNullOrEmpty(p.Value.ToString())))
                    parent = ((Document)node).ParentId > -1 ? ((Document)node).Parent : null;
                if (p != null)
                    int.TryParse(p.Value.ToString(), out result);
            }
            if (parent == null)
                return result;
            else
                return GetIntProperty(parent, propName, recursive);
        }
        public static bool GetBoolProperty(object node, string propName)
        {
            return GetBoolProperty(node, propName, false);
        }
        public static bool GetBoolProperty(object node, string propName, bool recursive)
        {
            bool result = false;
            if (node == null)
                return result;

            object parent = null;
            Type nodeType = node.GetType();
            if (nodeType.Equals(typeof(Node)))
            {
                IProperty p = ((Node)node).GetProperty(propName);
                if (recursive && (p == null || String.IsNullOrEmpty(p.Value.ToString())))
                    parent = ((Node)node).Parent;
                result = (p != null ? p.Value.ToString() == "1" : false);
            }
            else if (nodeType.Equals(typeof(Document)))
            {
                umbraco.cms.businesslogic.property.Property p = ((Document)node).getProperty(propName);
                if (recursive && (p == null || String.IsNullOrEmpty(p.Value.ToString())))
                    parent = ((Document)node).ParentId > -1 ? ((Document)node).Parent : null;
                result = (p != null ? p.Value.ToString() == "1" : false);
            }

            if (parent == null)
                return result;
            else
                return GetBoolProperty(parent, propName, recursive);
        }

    }
}
